
package com.atguigu.pratice2;


//线程间通信    线程 操作 资源类
//            判断 干活  通知
//        多线程通信中，如果存在wait方法的调用要避免   -----虚假唤醒


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// 有一个初始值为零的变量，现有两个线程对该变量操作，
// * 实现一个线程对变量加1，实现一个线程对变量减1,交替来10轮
//资源类
class AirCondition{
    private int  num=0;
    //老版 synchronized + wait + notifyall
//    public synchronized void increment() throws InterruptedException {
//        //判断
//        while(num!=0){
//            this.wait();
//        }
//        //干活
//
//        System.out.println(Thread.currentThread().getName()+"\t"+(++num));
//
//        //通知
//        this.notifyAll();
//    }
//    public synchronized void decrement() throws InterruptedException {
//        //判断
//        while(num!=1){
//            this.wait();
//        }
//        //干活
//
//        System.out.println(Thread.currentThread().getName()+"\t"+(--num));
//        //通知
//        this.notifyAll();
//    }

    //新版 lock  + condition.await + condition.signal

    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    public  void increment() throws InterruptedException {
        lock.lock();
        try{
            //判断
            while(num!=0){
                condition.await();
            }
            //干活
            System.out.println(Thread.currentThread().getName()+"\t"+(++num));
            //通知
            condition.signalAll();
        }finally {
            lock.unlock();
        }


    }
    public  void decrement() throws InterruptedException {
        lock.lock();
        try{
            //判断
            while(num==0){
                condition.await();
            }
            //干活
            System.out.println(Thread.currentThread().getName()+"\t"+(--num));
            //通知
            condition.signalAll();
        }finally{
            lock.unlock();
        }

    }

}
public class Thread_correspondence {
    public static void main(String[] args) {
        AirCondition airCondition = new AirCondition();
        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    airCondition.increment();
                }
            } catch (InterruptedException e) {
            }
        }, "A").start();
        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    airCondition.decrement();
                }
            } catch (InterruptedException e) {

            }
        }, "B").start();
        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    airCondition.increment();
                }
            } catch (InterruptedException e) {

            }
        }, "C").start();
        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    airCondition.decrement();
                }
            } catch (InterruptedException e) {

            }
        }, "D").start();

    }


}
